/**
 *
 * .enet360_upload_images		上传图片（本地+网络）
 * .enet360_upload_pc_images	上传图片（本地）
 * .enet360_upload_fail			上传附件
 * .enet360_upload_all_img		批量上传图片
 * 
**/
KindEditor.ready(function(K){
	
	
	var enet_kind_editor = [];
	$(".enet360_kind_editor").each(function(){
		var name = $(this).attr("name");
		enet_kind_editor[name] = K.create("textarea[name='"+name+"']", {
			uploadJson : Kind_upload_json,
			fileManagerJson : Kind_file_manager_json,
			allowFileManager : true,
			filterMode : false,
            items : [
                'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
                'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
                'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
                'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
                'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
                'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage',
                'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
                'anchor', 'link', 'unlink', '|', 'about'
            ],
			afterBlur: function(){enet_kind_editor[name].sync();}
		});		
	})
	var enet_kind_editor_min = [];
	$(".enet360_kind_editor_min").each(function(){
		var name = $(this).attr("name");
		enet_kind_editor_min[name] = K.create("textarea[name='"+name+"']", {
			uploadJson : Kind_upload_json,
			fileManagerJson : Kind_file_manager_json,
			allowFileManager : true,
			filterMode : false,
			items : ['fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline','removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist','insertunorderedlist'],
			afterBlur: function(){enet_kind_editor_min[name].sync();}
		});		
	})
	
	var enet_kind_editor_simple = [];
	$(".enet360_kind_editor_simple").each(function(){
		var name = $(this).attr("name");
		enet_kind_editor_simple[name] = K.create("textarea[name='"+name+"']", {
			uploadJson : Kind_upload_json,
			fileManagerJson : Kind_file_manager_json,
			allowFileManager : true,
			filterMode : false,
			items : ['fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline','removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist','insertunorderedlist'],
			afterBlur: function(){enet_kind_editor_simple[name].sync();}
		});		
	})
	
	
	
	
	var editor_upload = K.editor({
		uploadJson : Kind_upload_json,
		fileManagerJson : Kind_file_manager_json,
		allowFileManager : true,
		afterBlur: function(){editor_upload.sync();}
	});
	// 上传图片
	K('.enet360_upload_images').click(function(){
		var _name = $(this).attr("lay-name");
		var _width = $(this).attr("lay-width");
		var _height = $(this).attr("lay-height");
		var _pathname = $(this).attr("lay-pathname");
		
		var up_id = $(this).attr("varid");
		editor_upload.loadPlugin('image', function(){
			editor_upload.plugin.imageDialog({
				imageUrl : K('#input_'+_name+'').val(),
				clickFn : function(url,title,width,height,border,align){
					K('#input_'+_name).val(url);
					$('#img_'+_name).attr('src',url);
					editor_upload.hideDialog();
				}
			});
		});
	});
	//批量上传图片	
	K('.enet360_upload_pc_images').click(function(){
		var _name = $(this).attr("lay-name");
		var _width = $(this).attr("lay-width");
		var _height = $(this).attr("lay-height");
		var _pathname = $(this).attr("lay-pathname");
		
		editor_upload.loadPlugin('multiimage',function(){
			editor_upload.plugin.multiImageDialog({
				clickFn : function(urlList) {
					var div = $('#div_'+_name);
					var _num = 0;
					div.find('.all_imgs').each(function(){
						if(_num < parseInt($(this).attr("var"))){
							_num = parseInt($(this).attr("var"));
						}
					});
					_num++;
					K.each(urlList, function(i, data){
						var _html = '';
							_html += '<li var="'+(parseInt(_num)+parseInt(i))+'"><i>×</i>';
							_html += '<div class="layui-img"><img src="' + data.url + '" alt="点击删除该图片"></div>';
							_html += '<div class="layui-inputtext"><input type="text" name="'+_name+'['+(parseInt(_num)+parseInt(i))+'][name]" value="' + data.new_name + '" class="layui-input" placeholder="名称"></div>';
							_html += '<input type="hidden" name="'+_name+'['+(parseInt(_num)+parseInt(i))+'][url]" value="' + data.url + '"></li>';
					
						div.append(_html);
					});
					editor_upload.hideDialog();
				}
			});
		});
	});
	
	
	// 上传附件
	K('.enet360_upload_fail').click(function(){
		var up_id = $(this).attr("varid");
		editor_upload.loadPlugin('insertfile',function(){
			editor_upload.plugin.fileDialog({
				fileUrl : K('#'+up_id+'_input').val(),
				clickFn : function(url, title) {
					K('#'+up_id+'_input').val(url);
					editor_upload.hideDialog();
				}
			});
		});
	});	
});